﻿using System;
using System.Collections.Generic;
using System.Web;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;

namespace PDFConverterTest.Convertors
{
    public class WordPDFConvertor:IPDFConvertor
    {
        #region Ctor

        public WordPDFConvertor()
        {
            Application = new Word.ApplicationClass();
        }
        
        #endregion

        #region Properties

        public Word.Application Application
        {
            get;
            protected set;
        }
        #endregion

        #region Convertor Interface

        public void ConvertToPDF(string fileFullName, string pdfFullName)
        {
            Word.Document doc = null;
            try
            {
                int index = fileFullName.LastIndexOf(".");
                string fileExtension = fileFullName.Substring(index, fileFullName.Length - index);
                object openFileName = fileFullName;
                object openFileFormat = PDFConvertorFactory.GetOpenFileFormat<WordPDFConvertor>(fileExtension);
                object openMissing = Type.Missing;
                doc = Application.Documents.Open(ref openFileName, ref openMissing, ref openMissing, ref openMissing, ref openMissing, ref openMissing,
                        ref openMissing, ref openMissing, ref openMissing, ref openFileFormat, ref openMissing, ref openMissing, ref openMissing, ref openMissing, ref openMissing, ref openMissing);
                
                if (doc != null)
                {
                    object saveFileName = fileFullName.Substring(0, index) + PDFConvertorConstants.PDFFormat;
                    object saveFileForamt = PDFConvertorFactory.GetSaveFileFormat<WordPDFConvertor>(fileExtension);
                    object saveMissing = Type.Missing;
                    //doc.SaveAs2(ref saveFileName, ref saveFileForamt, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing,
                    //    ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing, ref saveMissing);
                    doc.ExportAsFixedFormat((string)saveFileName, Word.WdExportFormat.wdExportFormatPDF);
                    doc.Close(); 
                }
            }
            catch (Exception error)
            {
            }
            finally
            {
                Marshal.ReleaseComObject(doc);
                GC.Collect();
            }
        }

        public void ConvertToPDF(string fileFullName)
        {
            int index = fileFullName.LastIndexOf(".");
            if (index > -1)
            {
                string pdfFullName = fileFullName.Substring(0, index) + PDFConvertorConstants.PDFFormat;
                ConvertToPDF(fileFullName, pdfFullName);
            }
        }

        #endregion

        #region IDispose Interface

        public void Dispose()
        {
            Marshal.ReleaseComObject(Application);
            GC.Collect();
        }

        #endregion

    }
}